package com.myComsole.deepseek;

import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONUtil;
import com.myComsole.deepseek.model.DsSessionModel;
import com.myComsole.deepseek.model.Conversations;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @description:
 * @Date 2025/8/20 17:54
 * @Author huangfaxiang
 */
public class DsHandle {
    private  String sourcePath = "D:\\deepseek对话\\conversations.json";
    private  Conversations conversations;

    public static DsHandle build(String sourcePath){
        DsHandle dsHandle = new DsHandle();
        dsHandle.sourcePath=sourcePath;
        dsHandle.openFile();
        return dsHandle;
    }
    public static DsHandle buildDefault(){
        DsHandle dsHandle = new DsHandle();
        dsHandle.openFile();
        return dsHandle;
    }

    private void openFile() {
        File file = new File(sourcePath);
        String text = FileUtil.readString(file, StandardCharsets.UTF_8);
        conversations = JSONUtil.toBean(text, Conversations.class);
    }

    public List<String> showAllTitle() {
        List<DsSessionModel> sessions = conversations.getSessions();
        List<String> titles = sessions.stream().map(DsSessionModel::getTitle).collect(Collectors.toList());
        return titles;
    }


    private DsSessionModel selectByTitle(String title){
        List<DsSessionModel> sessions = conversations.getSessions();
        for (DsSessionModel session : sessions) {
            if (session.getTitle().contains(title)){
                return session;
            }
        }
        return null;
    }
}
